/// 用户评论模型
class UserComment {
  final int commentId;
  final String commentTitle;
  final String commentContent;
  final List<String> commentImages;
  final List<String> commentCategory;
  final List<String> commentTags;
  final String commentLocation;
  final double? longitude;
  final double? latitude;
  final String commentPublishTime;
  final List<String> shoppingImages;
  final int publisherId;
  final String publisherAvatar;
  final String publisherNickname;
  final String publisherSignature;
  final List<dynamic> publisherTags;
  final int publisherPoints;
  final int likeCount;
  final int commentCount;
  final int shareCount;
  final List<String> status;
  final List<Map<String, dynamic>> extraData;
  final String updatedAt;

  UserComment({
    required this.commentId,
    required this.commentTitle,
    required this.commentContent,
    required this.commentImages,
    required this.commentCategory,
    required this.commentTags,
    required this.commentLocation,
    this.longitude,
    this.latitude,
    required this.commentPublishTime,
    required this.shoppingImages,
    required this.publisherId,
    required this.publisherAvatar,
    required this.publisherNickname,
    required this.publisherSignature,
    required this.publisherTags,
    required this.publisherPoints,
    required this.likeCount,
    required this.commentCount,
    required this.shareCount,
    required this.status,
    required this.extraData,
    required this.updatedAt,
  });

  factory UserComment.fromJson(Map<String, dynamic> json) {
    return UserComment(
      commentId: json['commentId'] ?? 0,
      commentTitle: json['commentTitle'] ?? '',
      commentContent: json['commentContent'] ?? '',
      commentImages: _parseStringList(json['commentImages']),
      commentCategory: _parseStringList(json['commentCategory']),
      commentTags: _parseStringList(json['commentTags']),
      commentLocation: json['commentLocation'] ?? '',
      longitude: json['longitude']?.toDouble(),
      latitude: json['latitude']?.toDouble(),
      commentPublishTime: json['commentPublishTime'] ?? '',
      shoppingImages: _parseStringList(json['shoppingImages']),
      publisherId: json['publisherId'] ?? 0,
      publisherAvatar: json['publisherAvatar'] ?? '',
      publisherNickname: json['publisherNickname'] ?? '',
      publisherSignature: json['publisherSignature'] ?? '',
      publisherTags: _parseDynamicList(json['publisherTags']),
      publisherPoints: json['publisherPoints'] ?? 0,
      likeCount: json['likeCount'] ?? 0,
      commentCount: json['commentCount'] ?? 0,
      shareCount: json['shareCount'] ?? 0,
      status: _parseStringList(json['status']),
      extraData: _parseExtraDataList(json['extraData']),
      updatedAt: json['updated_at'] ?? '',
    );
  }

  /// 解析字符串列表，处理各种数据格式（包括null值）
  static List<String> _parseStringList(dynamic data) {
    if (data == null) return [];
    
    try {
      if (data is List) {
        // 过滤掉null值，并将其他值转换为字符串
        return data
            .where((item) => item != null)  // 过滤null值
            .map((item) => item.toString())
            .where((str) => str.isNotEmpty && str != 'null')  // 过滤空字符串和'null'字符串
            .toList();
      } else if (data is String && data.isNotEmpty && data != 'null') {
        return [data];
      }
    } catch (e) {
      print('解析字符串列表出错: $e, data: $data');
    }
    
    return [];
  }

  /// 解析动态类型列表，处理各种数据格式（包括null值）
  static List<dynamic> _parseDynamicList(dynamic data) {
    if (data == null) return [];
    
    try {
      if (data is List) {
        // 过滤掉null值
        return data.where((item) => item != null).toList();
      } else if (data != null) {
        return [data];
      }
    } catch (e) {
      print('解析动态列表出错: $e, data: $data');
    }
    
    return [];
  }

  /// 解析额外数据列表
  static List<Map<String, dynamic>> _parseExtraDataList(dynamic data) {
    if (data == null) return [];
    
    try {
      if (data is List) {
        return data
            .where((item) => item != null)
            .map((e) => Map<String, dynamic>.from(e is Map ? e : {}))
            .toList();
      } else if (data is Map) {
        return [Map<String, dynamic>.from(data)];
      }
    } catch (e) {
      print('解析额外数据列表出错: $e, data: $data');
    }
    
    return [];
  }

  Map<String, dynamic> toJson() {
    return {
      'commentId': commentId,
      'commentTitle': commentTitle,
      'commentContent': commentContent,
      'commentImages': commentImages,
      'commentCategory': commentCategory,
      'commentTags': commentTags,
      'commentLocation': commentLocation,
      'longitude': longitude,
      'latitude': latitude,
      'commentPublishTime': commentPublishTime,
      'shoppingImages': shoppingImages,
      'publisherId': publisherId,
      'publisherAvatar': publisherAvatar,
      'publisherNickname': publisherNickname,
      'publisherSignature': publisherSignature,
      'publisherTags': publisherTags,
      'publisherPoints': publisherPoints,
      'likeCount': likeCount,
      'commentCount': commentCount,
      'shareCount': shareCount,
      'status': status,
      'extraData': extraData,
      'updated_at': updatedAt,
    };
  }
}
